package cn.edu.sbs.talent.common.util;

import cn.hutool.crypto.digest.BCrypt;

/**
 * Bcrypt 加密相关工具
 *
 * @author ctidy
 * @since 2021/1/7
 */
public class BcryptUtils {

    private BcryptUtils() {}

    /**
     * 明文加密
     *
     * @param plain 明文
     * @return 密文
     */
    public static String encode(String plain) {
        return BCrypt.hashpw(plain, BCrypt.gensalt());
    }

    /**
     * 明文与密文匹配
     *
     * @param text 待匹配明文
     * @param cipher 密文
     * @return 匹配成功则为 {@code true}, 否则为 {@code false}
     */
    public static boolean match(String text, String cipher) {
        return BCrypt.checkpw(text, cipher);
    }

    public static void main(String[] args) {
        String password = "123456";
        String psd1 = encode(password);
        System.out.println("psd1 = " + psd1);
        String psd2 = encode(password);
        System.out.println("psd2 = " + psd2);
        System.out.println("psd1 == psd2 ? : " + psd1.equals(psd2));
        System.out.println("match(pad1) ? : " + match(password, psd1));
        System.out.println("match(psd2) ? : " + match(password, psd2));

    }

}
